package cn.chenfei.jfx.login;

import cn.chenfei.jfx.App;
import cn.chenfei.jfx.common.JFUtils;
import javafx.animation.TranslateTransition;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.util.Duration;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * @author ChenFei
 * @date 2022/4/10
 */
public class Login_4 extends StackPane {

    private StackPane container = new StackPane();
    //封面
    private StackPane coverPane = new StackPane();
    private Button loginBut = new Button("登录");
    //右侧区域
    private VBox mainContainer = new VBox();

    {
        this.getChildren().add(container);
        //
        container.getChildren().add(mainContainer);
        container.setAlignment(mainContainer, Pos.CENTER_RIGHT);
        //
        coverPane.getChildren().add(loginBut);
        container.getChildren().add(coverPane);
        container.getStyleClass().add("container");
        mainContainer.getStyleClass().add("main-container");
        coverPane.getStyleClass().add("cover-pane");
        loginBut.getStyleClass().add("login-but");
        this.getStylesheets().add(App.class.getResource("css/Login_4.css").toExternalForm());
        //拖动
        JFUtils.dragCurrentWindow(container);
    }

    private HBox topBox = new HBox();
    //最小化，关闭窗口按钮
    private Button hideBut = new Button();
    private Button closeBut = new Button();

    {
        container.getChildren().addAll(topBox);
        container.setAlignment(topBox, Pos.TOP_RIGHT);
        topBox.setMaxWidth(0);
        topBox.setMaxHeight(0);
        topBox.getStyleClass().add("top-box");
        topBox.getChildren().addAll(hideBut, closeBut);
        hideBut.setGraphic(new FontIcon(AntDesignIconsOutlined.MINUS));
        closeBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        hideBut.getStyleClass().add("hide-but");
        closeBut.getStyleClass().add("close-but");
        hideBut.setOnMouseClicked(event -> JFUtils.setIconified(event.getSource(),true));
        closeBut.setOnMouseClicked(event -> JFUtils.closeCurrentWindow(event.getSource()));
    }

    //logo
    private Pane logo = new Pane();
    //表单内容
    private Label titleLabel = new Label("你好！欢迎登录");
    private HBox thirdBox = new HBox();
    private Label wbLabel = new Label();
    private Label zfbLabel = new Label();
    private Label qqLabel = new Label();
    private Label ddLabel = new Label();
    private Label emailLabel = new Label("使用你的邮箱登录");

    {
        mainContainer.getChildren().addAll(logo, titleLabel, thirdBox);
        mainContainer.setAlignment(Pos.CENTER);
        thirdBox.getChildren().addAll(wbLabel, zfbLabel, qqLabel, ddLabel);
        wbLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.WEIBO_CIRCLE));
        zfbLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.ALIPAY_CIRCLE));
        qqLabel.setGraphic(new FontIcon(AntDesignIconsFilled.QQ_CIRCLE));
        ddLabel.setGraphic(new FontIcon(AntDesignIconsFilled.DINGTALK_CIRCLE));
        wbLabel.getStyleClass().add("wb-label");
        zfbLabel.getStyleClass().add("zfb-label");
        qqLabel.getStyleClass().add("qq-label");
        ddLabel.getStyleClass().add("dd-label");
        titleLabel.getStyleClass().add("title-label");
        thirdBox.getStyleClass().add("third-box");
        //提示语
        Tooltip tooltip = new Tooltip("微博账号登录");
        tooltip.setShowDelay(Duration.millis(50));
        wbLabel.setTooltip(tooltip);
        //邮箱登录方式
        emailLabel.getStyleClass().add("email-label");
        mainContainer.getChildren().add(emailLabel);
        //logo
        logo.getStyleClass().add("logo");

    }

    //文本框，密码框
    /**
     * 输入框布局-----------------------------------------------------------
     */
    private HBox userBox = new HBox();
    private Label userLabel = new Label();
    private TextField userField = new TextField();
    //
    private HBox pswBox = new HBox();
    private Label pswLabel = new Label();
    private PasswordField pswField = new PasswordField();
    //
    private Button forget = new Button("忘记密码？");
    private Button login = new Button("登录");
    //提示语
    private Label message = new Label();

    {
        mainContainer.getChildren().addAll(userBox, pswBox, forget, login, message);
        //
        userBox.getChildren().addAll(userLabel, userField);
        userBox.setHgrow(userField, Priority.ALWAYS);
        userBox.getStyleClass().add("user-box");
        userLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.MAIL));
        userLabel.getStyleClass().add("user-label");
        userField.setPromptText("邮箱");
        userField.getStyleClass().add("user-field");
        //
        pswBox.getChildren().addAll(pswLabel, pswField);
        userBox.setHgrow(pswField, Priority.ALWAYS);
        pswBox.getStyleClass().add("psw-box");
        pswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.LOCK));
        pswLabel.getStyleClass().add("psw-label");
        pswField.getStyleClass().add("psw-field");
        pswField.setPromptText("密码");
        forget.getStyleClass().add("forget");
        login.getStyleClass().add("login");
        //提示语
        message.getStyleClass().add("message");
    }

    private TranslateTransition translate1 = new TranslateTransition(Duration.seconds(1), coverPane);//移动动画
    private TranslateTransition translate2 = new TranslateTransition(Duration.seconds(1), mainContainer);//移动动画

    public Login_4() {
        //封面动画
        loginBut.setOnMouseClicked(event -> {
            //设置动画要移动到的位置
            translate1.setFromX(0);
            translate1.setToX(coverPane.getWidth() / 2);
            //
            translate2.setFromX(0);
            translate2.setToX(0 - mainContainer.getWidth());
            //开始动画
            translate1.play();
            translate2.play();
            translate1.setOnFinished(actionEvent -> loginBut.setVisible(false));
        });
        //
        //输入框选中事件绑定
        userField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            userBox.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        pswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "base-color;" : "rgb(182,182,182);";
            pswBox.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        //
        EventHandler eventUrl = (EventHandler<Event>) e -> new App().getHostServices().showDocument("https://openjfx.cn/");
        forget.setOnMouseClicked(eventUrl);
        //登录点击事件
        login.setOnMouseClicked(event -> {
            event.consume();
            if (Cursor.WAIT.equals(login.getCursor())) {
                return;
            }
            String username = userField.getText();
            String password = pswField.getText();

            System.out.println(String.format("username: %s ; password: %s ;", username, password));
            if (username.trim().equals("")) {
                message.setText("邮箱不能为空");
                return;
            }
            if (password.trim().equals("")) {
                message.setText("密码不能为空");
                return;
            }
            message.setText("");
            login.setCursor(Cursor.WAIT);

        });

    }

}
